package com.yazq.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yazq.mapper.EmpExprMapper;
import com.yazq.mapper.EmpMapper;
import com.yazq.pojo.Emp;
import com.yazq.pojo.Emp_expr;
import com.yazq.pojo.PageResult;
import com.yazq.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Override
    public PageResult<Emp> getEmpInfoService(String name, Integer gender, LocalDateTime begin, LocalDateTime end, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Emp> list = empMapper.getInfo(name, gender, begin, end);
        Page<Emp> result =(Page<Emp>)list;
        return new PageResult<Emp>(result.getTotal(), result.getResult());
    }
    public void addEmp(Emp emp){
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insertEmp(emp);
        List<Emp_expr> exprList = emp.getExprList();
        if(exprList!= null && !exprList.isEmpty()){
            exprList.forEach(expr -> {
                expr.setEmp_id(emp.getId());
            });
            empExprMapper.insertExpr(exprList);
        }
    }
}
